/*
 * @Author: Trae AI
 * @Date: 2024-01-15
 * @Description: AI服务
 */
import { Injectable } from '@nestjs/common';
import OpenAI from 'openai';
import { ChatDto } from './dto/chat.dto';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class AiService {
    private openai: OpenAI;

    constructor(private configService: ConfigService) {
        this.openai = new OpenAI({
            baseURL: 'https://api.deepseek.com',
            apiKey: this.configService.get<string>('DEEPSEEK_API_KEY'),
        });
    }

    async chat(chatDto: ChatDto) {
        try {
            const completion = await this.openai.chat.completions.create({
                messages: [
                    { role: 'system', content: 'You are a helpful assistant.' },
                    { role: 'user', content: chatDto.message },
                ],
                model: 'deepseek-chat'
            });

            return {
                content: completion.choices[0].message.content,
            };
        } catch (error) {
            throw new Error('AI服务调用失败：' + error.message);
        }
    }
}